package com.ruoyi.car.controller.admin.carinfo;

import com.github.pagehelper.PageInfo;
import com.ruoyi.car.controller.admin.carinfo.vo.*;
import com.ruoyi.car.controller.admin.category.vo.CarCategoryResp;
import com.ruoyi.car.controller.admin.company.vo.CarCompanyResp;
import com.ruoyi.car.controller.admin.model.vo.CarModelAddReq;
import com.ruoyi.car.entity.CarCategory;
import com.ruoyi.car.entity.CarCompany;
import com.ruoyi.car.entity.CarModel;
import com.ruoyi.car.es.entity.EsCarInfo;
import com.ruoyi.car.es.service.EsCarInfoService;
import com.ruoyi.car.mapstruct.CarCategoryTo;
import com.ruoyi.car.mapstruct.CarCompanyConvert;
import com.ruoyi.car.mapstruct.CarModelConvert;
import com.ruoyi.car.service.CarInfoPhotoService;
import com.ruoyi.car.service.CarInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/6/7 20:01
 * @Version 1.0
 */
@Api(tags = "管理后台-车辆信息")
@RestController
@RequestMapping(value = "/car/info")
public class CarInfoController {

    @Autowired
    private CarInfoService carInfoService;

    @Autowired
    private CarInfoPhotoService  carInfoPhotoService;

    @Autowired
    private EsCarInfoService esCarInfoService;

    @ApiOperation(value = "分页查询")
    @PreAuthorize(value = "@ss.hasPermi('car:info:page')")
    @GetMapping(value = "/page")
    public TableDataInfo search(CarInfoReq req){
        //数据库查询
        PageInfo<CarInfoResp> pageInfo=this.carInfoService.search(req);
        TableDataInfo tableDataInfo=new TableDataInfo(pageInfo.getList(), pageInfo.getTotal());
        return tableDataInfo;
    }

    @ApiOperation(value = "ES分页查询")
    @PreAuthorize(value = "@ss.hasPermi('car:info:page')")
    @GetMapping(value = "/page_es")
    public TableDataInfo searchES(CarInfoReq req) throws IOException {
        //数据库查询
        TableDataInfo tableDataInfo= this.esCarInfoService.search(req);
        return tableDataInfo;
    }





    @ApiOperation(value = "根据图片ID删除汽车图标")
    @PreAuthorize(value = "@ss.hasPermi('info:car:edit')")
    @DeleteMapping(value = "/remove/photo/{id}")
    public R removeCarPhoto(@PathVariable(value = "id") Long id){
        boolean result = this.carInfoPhotoService.removeById(id);
        return  result? R.ok(): R.fail();
    }

    @Log(title = "车辆信息",businessType = BusinessType.INSERT)
    @PreAuthorize(value = "@ss.hasPermi('car:info:add')")
    @ApiOperation(value = "新增")
    @PostMapping
    public R  add(@RequestBody @Valid CarInfoAddReq req){
        boolean save = this.carInfoService.add(req);
        return save? R.ok(): R.fail();
    }


    @PreAuthorize(value = "@ss.hasPermi('car:info:query')")
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/{id}")
    public R<CarInfoDetailResp> findById(@PathVariable(value = "id") Long id){
        CarInfoDetailResp db = this.carInfoService.findById(id);
        return R.ok(db);
    }

    @Log(title = "车辆信息",businessType = BusinessType.UPDATE)
    @PreAuthorize(value = "@ss.hasPermi('info:car:edit')")
    @ApiOperation(value = "修改")
    @PutMapping
    public R  update(@RequestBody @Valid CarInfoEditReq req){
        boolean save = this.carInfoService.edit(req);
        return save? R.ok(): R.fail();
    }

    @ApiOperation(value = "同步数据库的值到ES库中")
    @Log(title = "车辆信息",businessType = BusinessType.IMPORT)
    @PreAuthorize(value = "@ss.hasPermi('info:car:edit')")
    @GetMapping(value = "/loadAllDataToEs")
    public R loadAllDataToEs() throws IOException, InterruptedException {
        this.esCarInfoService.reloadCarInfoToEs();
        return R.ok();
    }


}
